package pl.jad.database.table.column;

import java.lang.reflect.Field;

import pl.jad.database.table.Table;

/**
 * Kolumna przechowujaca dane pewnego obiektu - klucz obcy do tabeli z jego danymi
 * 
 * @author paq
 *
 */
public class ObjectTableColumn extends TableColumn {
	public ObjectTableColumn(Table table, Field field) {
		super(table, field);
		
		this._name = field.getName();
	}
	
	/*public ObjectTableColumn(Table table, String name) {
		this._name = name;
		this._type = getType(int.class);
	}*/
	
	public String getSqlFieldValue(Object object) throws Exception {
		boolean accessible = getField().isAccessible();
		if (!accessible) {
			getField().setAccessible(true);
		}
		
		if (isEmptyField(object)) {
			return "null";
		}
		
		int index = _table.getMapper().save(this._field.get(object));
		 _Log.info("Object saved in another table - foreign key = " + index);
		
		if (!accessible) {
			getField().setAccessible(false);
		}
		
		return Integer.toString(index);
	}
	
	
}
